package com.yorick;

import com.yorick.logic.service.SyncAudioScheduler;
import com.yorick.logic.service.SyncScheduler;
import com.yorick.netty.EchoServer;
import com.yorick.netty.WebSocketServer;
import com.yorick.netty.messageListener.Register;
import com.yorick.netty.utils.SysConfig;

import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NettyLauncher {
    private final static boolean autoStart = Boolean.parseBoolean(SysConfig.getInstance().getProperty("autoStart"));
    private final static boolean autoStartWebsocket = Boolean.parseBoolean(SysConfig.getInstance().getProperty("autoStart_websocket"));

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        processRegisterMessage();
        if (autoStart){
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    EchoServer server = new EchoServer();
                    server.start();
                }
            });
        }
        if(autoStartWebsocket){
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    WebSocketServer server = new WebSocketServer();
                    server.start();
                }
            });
        }
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new SyncScheduler(),33,33);
        timer.scheduleAtFixedRate(new SyncAudioScheduler(),1000,1000);
    }

    private static void processRegisterMessage(){
        Register register = Register.getInstance();
        register.registerMessage(0,"LoginServiceImpl");
        register.registerMessage(1,"LocationServiceImpl");
        register.registerMessage(3,"AudioServiceImpl");
        register.invoke();
    }
}
